﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hi_Baidu.Packet;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Timer;
using System.Windows.Controls.Primitives;

namespace Hi_Baidu
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            cls_Application_Init.Init();
            InitializeComponent();
            cls_Application_Init.CheckReferenceFile(this);
            this.InitializeComponent();
            Globle.win_Login = this;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(e.ChangedButton == MouseButton.Left)
                this.DragMove();
        }

        private void btn_Login_Click(object sender, RoutedEventArgs e)
        {
            //登陆
            if(this.btn_Login.Content.ToString() == "登陆")
            {
                byte[] username = Encoding.Default.GetBytes(this.cmb_Username.Text.Trim());
                byte[] password = Encoding.Default.GetBytes(this.password1.Password.Trim());
                byte[] OnlineState = BitConverter.GetBytes(int.Parse((this.cb_OnlineState.SelectedItem as ComboBoxItem).Tag.ToString()));
                byte[] Package = LoginProcessModel.LoginParser(username, password, OnlineState);
                if (Package != null)
                {
                    Globle.TEMP_USER_SETTING = new Hi_Baidu.User.Serializable.Log.UserSettingInfo("../Users/" + this.cmb_Username.Text.Trim() + "/Info.db", "USER_SETTING_INFO");
                    Globle.TEMP_USER_SETTING.Username = this.cmb_Username.Text.Trim();
                    Globle.TEMP_USER_SETTING.Password = this.password1.Password.Trim();
                    Globle.TEMP_USER_SETTING.LoginState = int.Parse((this.cb_OnlineState.SelectedItem as ComboBoxItem).Tag.ToString());
                    Globle.TEMP_USER_SETTING.AutoLogin = (bool)this.ckb_AutoLogin.IsChecked;
                    Globle.TEMP_USER_SETTING.RememberPassword = (bool)this.ckb_RememberPassword.IsChecked;
                    LoginProcessModel.ChangeState(true, this);
                    LoginProcessModel.Login(Package);     
                    //开始登陆超时计算
                    LoginTimeOutEvent LTOE = new LoginTimeOutEvent("LoginTimerOut", 3000, 1, username, password, OnlineState, this);
                    Globle.timeoutTimerManager.InstallTimer(LTOE);
                }
            }
            //取消
            else
            {
               LoginProcessModel.CancleLogin(this);
               Globle.TEMP_USER_SETTING = null;
            }
        }

        private void cmb_Username_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Globle.win_Login != null && (sender as ComboBox).SelectedItem != null)
            {
                String Account = (sender as ComboBox).SelectedItem.ToString();
                Globle.USER_SELECT_LOGIN_NAME = Account;
                UserProcessModel.ProcessUserReference(Globle.win_Login, Account);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Globle.user.NI.Visible = false;
            Environment.Exit(0);
        }

        private void ComboBoxItem_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }
    }
}
